package com.cty;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Auther: cty
 * @Date: 2020/5/22 17:46
 * @Description: 测试solr增删查改
 *  SolrJ：Java操作Solr的API
 * @version: 1.0
 */
public class TestSolr {
    /**
     * 测试新增
     * @throws IOException
     * @throws SolrServerException
     */
    public static void solrInsert() throws IOException, SolrServerException {
        SolrClient client = new HttpSolrClient("http://192.168.238.132:6080/solr");

        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "001");
        doc.addField("solr_ch", "test solr_ch");
        doc.addField("solr_ch2", "test solr_ch2");

        client.add(doc);
        client.commit();

    }  // end method testInsert

    /**
     * 测试删除
     * @throws IOException
     * @throws SolrServerException
     */
    public static void solrDelete() throws IOException, SolrServerException {
        SolrClient client = new HttpSolrClient("http://192.168.238.132:6080/solr");

        client.deleteById("001");
        client.commit();
    }

    /**
     * 测试查询
     * @throws IOException
     * @throws SolrServerException
     */
    public static void solrQuery() throws IOException, SolrServerException {
        SolrClient client = new HttpSolrClient("http://192.168.238.132:6080/solr");

        // 可视化界面左侧条件
        SolrQuery params = new SolrQuery();
        // 设置查询内容
        params.setQuery("solr_all:*");
        // 设置分页
        params.setStart(0);  // 设置从第0条开始查询
        params.setRows(10);  // 设置查询10条记录
        // 设置查询高亮
        params.setHighlight(true);  // 启动高亮
        params.addHighlightField("solr_ch");  // 设置高亮字段
        params.setHighlightSimplePre("<span style='color:red'>");  // 设置前缀
        params.setHighlightSimplePost("</span>");  // 设置后缀

        // 查询
        // 相当于点击查询按钮, 本质是向solr web服务器发送请求,并接收响应. query对象里面包含了返回json数据
        QueryResponse response = client.query(params);

        // 从响应中取出 highlighting (高亮数据)
        Map<String, Map<String, List<String>>> hls = response.getHighlighting();
        // 从响应中取出 docs
        SolrDocumentList solrList = response.getResults();

        // 显示查询结果
        for(SolrDocument doc : solrList)
        {
            System.out.println(doc.getFieldValue("id"));
            System.out.println(doc.getFieldValue("solr_ch"));

            System.out.println("高亮：");
            Map<String, List<String>> hl = hls.get(doc.getFieldValue("id"));
            System.out.println(hl);
            List<String> list = hl.get("solr_ch");
            if(list!=null && list.size()>0)
                System.out.println("高亮："+list.get(0));
            else
                System.out.println("没有高亮内容");

            System.out.println("--------------------------------------");
        }

    }  // end method solrQuery

}  // end class TestSolr

/**
 *     "docs": [
 *       {
 *         "id": "001",
 *         "solr_ch": "test solr_ch",
 *         "solr_all": [
 *           "test solr_ch",
 *           "test solr_ch2"
 *         ],
 *         "solr_ch2": "test solr_ch2",
 *         "_version_": 1667427127548444700
 *       }
 *     ]
 *   },
 *   "highlighting": {
 *     "001": {
 *       "solr_ch": [
 *         "<em>test</em> <em>solr_ch</em>"
 *       ]
 *     }
 */
